package com.yangtzeu.jwc.activity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yangtzeu.jwc.domain.Kb;
import com.zhangdi.jwc.R;

public class TimetableActivity extends BaseActivity {
    private Kb kb;
    private ViewPager mPager;// 页卡内容
    private List<View> listViews; // Tab页面列表
    private TextView t1, t2, t3, t4, t5, t6, t7;// 页卡头标
    private List<TextView> kbHeads;
    private int currentWeek;// 记录现在所在星期位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    }

    @Override
    public void setView() {
	super.setView();
	setContentView(R.layout.activity_timetable);
    }

    @Override
    public void initData() {
	super.initData();
	Intent intent = getIntent();
	kb = (Kb) intent.getSerializableExtra("timetable");
    }

    @Override
    public void initActionBar() {
	super.initActionBar();
	setSimpleActionBar(null, "课表", null, null);
    }

    @Override
    public void initView() {
	super.initView();
	initTextView();
	initViewPager(kb);
    }

    /**
     * 初始化头标
     */
    private void initTextView() {
	List<TextView> textViews = new LinkedList<TextView>();
	t1 = (TextView) findViewById(R.id.monday);
	t1.setTextColor(Color.RED);
	t2 = (TextView) findViewById(R.id.tuesday);
	t3 = (TextView) findViewById(R.id.wednesday);
	t4 = (TextView) findViewById(R.id.thrusday);
	t5 = (TextView) findViewById(R.id.friday);
	t6 = (TextView) findViewById(R.id.saturday);
	t7 = (TextView) findViewById(R.id.sunday);

	textViews.add(t1);
	textViews.add(t2);
	textViews.add(t3);
	textViews.add(t4);
	textViews.add(t5);
	textViews.add(t6);
	textViews.add(t7);

	for (int i = 0; i < textViews.size(); i++) {
	    textViews.get(i).setOnClickListener(new MyOnClickListener(i));
	}
	this.kbHeads = textViews;
    }

    /**
     * 初始化ViewPager
     */
    @SuppressLint("InflateParams")
    private void initViewPager(Kb kb) {
	mPager = (ViewPager) findViewById(R.id.viewpager);
	listViews = new ArrayList<View>();
	LayoutInflater mInflater = getLayoutInflater();
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));
	listViews.add(mInflater.inflate(R.layout.kebiaolist, null));

	mPager.setAdapter(new MyPagerAdapter(getApplicationContext(), listViews, kb));
	mPager.setCurrentItem(0);
	mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
	private int index = 0;

	public MyOnClickListener(int i) {
	    index = i;
	}

	@Override
	public void onClick(View v) {
	    mPager.setCurrentItem(index);
	}
    }

    ;

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements OnPageChangeListener {

	@Override
	public void onPageSelected(int to) {
	    TextView fromhead = kbHeads.get(currentWeek);
	    TextView tohead = kbHeads.get(to);
	    tohead.setTextColor(Color.RED);
	    fromhead.setTextColor(Color.GRAY);
	    currentWeek = to;
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}
    }

    private final class MyPagerAdapter extends PagerAdapter {
	private List<View> mListViews;
	private Kb kb;
	private ListView currentListView;
	private Context context;

	public MyPagerAdapter(Context context, List<View> mListViews, Kb kb) {
	    this.mListViews = mListViews;
	    this.kb = kb;
	    this.context = context;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
	    ((ViewPager) arg0).removeView(mListViews.get(arg1));
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	@Override
	public int getCount() {
	    return mListViews.size();
	}

	@Override
	public Object instantiateItem(View arg0, int arg1) {
	    ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
	    currentListView = (ListView) mListViews.get(arg1).findViewById(R.id.kebiaolist);
	    currentListView.setAdapter(new TimeTableAdapter(context, kb.getKebiao(), currentListView, R.layout.kebiao_item, arg1));
	    return mListViews.get(arg1);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
	    return arg0 == (arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
	    return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}
    }

    /**
     * 显示课表的监听
     * 
     * @author ZhangDI
     */
    private final class TimeTableAdapter extends BaseAdapter {
	private String[][] kebiao;
	private int resource;
	private LayoutInflater inflater;
	private int xinqiji = 0;

	public TimeTableAdapter(Context context, String[][] kebiao, View listView, int resource, int xingqi) {
	    super();
	    this.kebiao = kebiao;
	    this.resource = resource;
	    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    this.xinqiji = xingqi;
	}

	@Override
	public int getCount() {
	    return kebiao.length;
	}

	@Override
	public Object getItem(int position) {
	    return this.kebiao[position][xinqiji];
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    if (convertView == null) {
		convertView = inflater.inflate(resource, null);
	    }
	    // 得到相应条目的控件
	    TextView jieci = (TextView) convertView.findViewById(R.id.jieci);

	    TextView kecheng = (TextView) convertView.findViewById(R.id.kecheng);
	    Object kebiaoInfo = getItem(position);
	    if (kebiaoInfo == null) {
		kebiaoInfo = "";
	    }

	    //L.d(kebiaoInfo.toString() + "------------" + kebiaoInfo.toString().length());
	    //convertView.setBackgroundColor(Color.rgb(0, 255, 128));
	    //if (kebiaoInfo.toString().length() <= 1) {
	//	L.d("设置没有课程的颜色!");
	//	convertView.setBackgroundColor(Color.rgb(200, 200, 200));
	//   }
	    jieci.setText(position + 1 + "");
	    kecheng.setText(kebiaoInfo.toString());
	    return convertView;
	}

    }
}
